package com.kb.erp.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kb.erp.common.ApiResponse;
import com.kb.erp.common.ResponseObject;
import com.kb.erp.domain.CrmCustomerDept;
import com.kb.erp.entity.flow.FlowJsonData;
import com.kb.erp.service.*;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * 通用controller
 */
@RestController
@RequestMapping("/common")
public class CommonController {

    @Resource
    private CommonService commonService;
    @Resource
    private FlowChartQueueService flowChartQueueService;
    @Resource
    private KBIDMService kbidmService;
    @Resource
    private CrmService crmService;
    @Resource
    private ItmaNageService itmaNageService;


    @GetMapping("/flowUpdateStatus")
    @XxlJob("flowUpdateStatus")
    public void queryResignInfo() {
        flowChartQueueService.updateStatus();
    }

    /**
     * 处理crm同步数据到crm CRM_SalesOrder表异常数据
     */
    @GetMapping("/erp2crmBySalesOrder")
    @XxlJob("erp2crmBySalesOrder")
    public void erp2crmBySalesOrder() {
        commonService.erp2crmBySalesOrder();
    }

    /**
     * 处理crm同步数据到crm CRM_SalesOrder表异常数据
     */
    @GetMapping("/crmCustInfo2IDM")
    @XxlJob("crmCustInfo2IDM")
    public void crmCustInfo2IDM(String code) {
        kbidmService.crmCustInfo2IDM(code);
    }

    /**
     * 特殊订单数据下载
     * @param flowJsonData
     * @param response
     */
    @PostMapping("/specialOrderProcess")
    public void specialOrderProcess(@RequestBody FlowJsonData flowJsonData, HttpServletResponse response) {
        flowChartQueueService.specialOrderProcess(flowJsonData,response);
    }

    /**
     * 道一云数据解析
     */
    @PostMapping("/flowDataSyn")
    @XxlJob("flowDataSyn")
    public void flowDataSyn() {
        flowChartQueueService.flowDataSyn();
    }

    /**
     * crm用户表工号数据维护
     */
    @PostMapping("/updateUserCode")
    @XxlJob("updateUserCode")
    public void updateUserCode() {
        crmService.updateUserCode();
    }

    /**
     * 更新INVT_INFO数据
     */
    @PostMapping("/synInvtInfo")
    @XxlJob("synInvtInfo")
    public void synInvtInfo() {
        itmaNageService.synInvtInfo();
    }
    @PostMapping("/importUser")
    public String importUser(@RequestPart("file") MultipartFile file) throws Exception {
            crmService.updateCustomerDept(file);
            return "更新成功";
    }
    /**
     *
     */
    @PostMapping("/queryCrmCustomerDept")
    public ApiResponse queryCrmCustomerDept(@RequestBody CrmCustomerDept customerDept) {
        ResponseObject responseObject = new ResponseObject();
        Page<CrmCustomerDept> crmCustomerDeptPage = crmService.queryCrmCustomerDept(customerDept);
        responseObject.setItems(crmCustomerDeptPage.getRecords());
        responseObject.setTotal(crmCustomerDeptPage.getTotal());
       return ApiResponse.success(responseObject);
    }
    /**
     *
     */
    @PostMapping("/updateCustomerDept")
    public ApiResponse updateCustomerDept(@RequestBody CrmCustomerDept customerDept) {
       crmService.updateCustomerDept(customerDept);
        return ApiResponse.success("更新成功");
    }
}
